package GOOGLE;

import java.io.File;

import android.graphics.Path;
import android.graphics.Point;
import android.widget.ImageView;

import com.fitsoftwareprocess.mobimap.dto.MapInfoEx;

public class GoogleMapProviderEx {
	
	
	 private String lang = "vi";// language 

     protected String map_url = "http://mt{0}.google.com/vt/hl={1}&x={2}&y={3}&z={4}";
     protected String satellite_url = "http://khm{0}.google.com/kh/v={1}&x={2}&y={3}&z={4}";
     protected String hybrid_url = "http://mt{0}.google.com/vt/lyrs=y&hl={1}&x={2}&y={3}&z={4}";
     protected String satellite_ver = "54";

     private ImageView[] image;
     //= new Image[3, 3];

     private int GetServerResponse(Point tile, int max)
     {
         return (tile.x + 2 * tile.y) % max;
     }
	
	
	public static String pathCombine(String path1, String path2)
	{
	    File parent = new File(path1);
	    File child = new File(parent, path2);
	    return child.getPath();
	}
	
	 private byte[] GetGoogleMap(MapInfoEx map)
     {
         if (map.CheckExist())
             return map.ConvertToByte();

         String strRequest="";
         //= String.Empty;
         switch (map.MapType)
         {
             case 0: //map
                 strRequest = String.format(map_url, GetServerResponse(map._tile, 4), lang, map._tile.x, map._tile.y, map._zoom);
                 break;
             case 1: //satellite
                 strRequest = String.format(satellite_url, GetServerResponse(map._tile, 4), satellite_ver, map._tile.x, map._tile.y, map._zoom);
                 break;
             case 2: //hybrid
                 strRequest = String.format(hybrid_url, GetServerResponse(map._tile, 4), lang, map._tile.x, map._tile.y, map._zoom);
                 break;
         }

         WebRequest webrequest = WebRequest.Create(strRequest);
         ((HttpWebRequest)webrequest).UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
         WebResponse webresponse = webrequest.GetResponse();
         Stream stream = webresponse.GetResponseStream();
         StreamReader streamReader = new StreamReader(stream);
         MemoryStream memStream = new MemoryStream();
         byte[] respBuffer = new byte[1024];
         int byteRead = stream.Read(respBuffer, 0, respBuffer.Length);
         while (byteRead > 0)
         {
             memStream.Write(respBuffer, 0, byteRead);
             byteRead = stream.Read(respBuffer, 0, respBuffer.Length);
         }
         return memStream.GetBuffer();
     }

	
	
	public ImageView GetMapImage(MapInfoEx map) {
		 map._mapStore= pathCombine(map._mapStore, "Google");
         InitListImage(map);
         Image memImage = CombineImage();
         Image currentImage = new Bitmap(map.Size.Width, map.Size.Height);
         Graphics g = Graphics.FromImage(currentImage);
         int k = map.MapCenter.X % 256;
         int t = map.MapCenter.Y % 256;
         int temp1 = 256 + k - currentImage.Width / 2;
         int temp2 = 256 + t - currentImage.Height / 2;
         g.DrawImage(memImage, new Rectangle(0, 0, currentImage.Width, currentImage.Height),
             new Rectangle(temp1, temp2, currentImage.Width, currentImage.Height), GraphicsUnit.Pixel);
         g.Dispose();

         return currentImage;
	}
	
	public void InitListImage(MapInfoEx map)
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                Point currentTile = new Point(map._tile.x + i - 1, map._tile.y + j - 1);
                MapInfoEx mapTemp = new MapInfoEx();
                //mapTemp.EarthPoint = map.EarthPoint;
                mapTemp._mapStore = map._mapStore;
                mapTemp._size = map._size;
                mapTemp.MapSource = map.MapSource;
                mapTemp._zoom = map._zoom;
                mapTemp._tile = currentTile;
                mapTemp.MapType = map.MapType;
                ImageView temp = map.GetMapFromByte(GetGoogleMap(mapTemp));
                if (!mapTemp.CheckExist())
                {
                    Bitmap bitmap = new Bitmap(temp);
                    bitmap.Save(mapTemp.GetFullPath(), ImageFormat.Png);
                    bitmap.Dispose();
                }
                temp.Dispose();
                image[i, j] = new Bitmap(mapTemp.GetFullPath());
            }
        }
    }

}


